<?php
/*
 * 商品品牌列表
 */
namespace app\admin\controller;
use app\common\controller\AdminBase;
use think\facade\Db;
use think\facade\Session;
use app\common\model\GoodBrand as GoodBrandModel;

class GoodBrand extends AdminBase
{
    public function lists()
    {
        $param = $this->request->param();
        if (!empty($param['title'])) {
            $where = function ($query) use ($param){$query->where('name','LIKE', '%'.trim($param['title']).'%');};
        }else{
            $where = 1;
        }
        $list = GoodBrandModel::where($where)->paginate();
        return $this->fetch('index',['list'=>$list]);
    }

    public function add()
    {
        if ($this->request->isPost()) {
            $param = $this->request->param();
            //验证规则
            $verify = input('_verify', true);
            if($verify!='0'){
                try{
                    $this->validate($param, 'menu');
                }catch (\Exception $e){
                    $this->error($e->getMessage());
                }
            }
            $result = GoodBrandModel::create($param);
            !$result && $this->error($this->errorMsg);
            insert_admin_log('添加了品牌');
            $this->success('添加成功');
        }
        return $this->fetch('save');
    }

    public function edit()
    {
        if ($this->request->isPost()) {
            $param = $this->request->param();
            //验证规则
            $verify = input('_verify', true);
            if($verify!='0'){
                try{
                    $this->validate($param, 'menu');
                }catch (\Exception $e){
                    $this->error($e->getMessage());
                }
            }
            $result = GoodBrandModel::update($param);
            !$result && $this->error($this->errorMsg);
            insert_admin_log('修改了品牌');
            $this->success('修改成功');
        }
        return $this->fetch('save',['data' => GoodBrandModel::find(input('id'))]);
    }

    public function del()
    {
        if ($this->request->isPost()) {
            $param = $this->request->param();
            Db::table('goods')->where('brand_id',$param['id'])->find() && $this->error('品牌下有商品，无法删除');
            GoodBrandModel::destroy($param['id']);
            insert_admin_log('删除了品牌');
            $this->success('删除成功');
        }
    }





}
